package cn.com.self.apis.member.controller;

import cn.com.self.apis.core.vo.ResultModel;
import cn.com.self.apis.member.entity.UReportInfo;
import cn.com.self.apis.member.entity.dto.ReportInfoDTO;
import cn.com.self.apis.member.entity.vo.UReportInfoVO;
import cn.com.self.apis.member.service.IReportFileService;
import cn.com.self.apis.member.service.IReportInfoService;
import cn.com.self.apis.portal.util.ICustomPage;
import cn.com.selfsoft.smartcity.commons.http.DataItem;
import cn.com.selfsoft.smartcity.commons.http.DataPage;
import cn.com.selfsoft.smartcity.commons.util.ParameterUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

/**
 * @author L
 * @title: ReportInfoController
 * @projectName self
 * @description: TODO
 * @date 2021/1/11 17:32
 */
@Controller
@RequestMapping("/reportInfo")
@Api(tags = "举报信息类")
public class ReportInfoController {
    @Autowired
    private IReportInfoService reportInfoService;
    @Autowired
    private IReportFileService reportFileService;
    /**新增举报
     * @Description //TODO
     * @return
     **/
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增举报信息")
    public ResponseEntity create(@RequestBody @Valid ReportInfoDTO entity, HttpServletRequest request){
        return reportInfoService.createReportInfo(entity);
    }


    /**分页获取举报列表
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "分页获取举报列表")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity getReportInfoPage(HttpServletRequest request, Pageable page){
        QueryWrapper<UReportInfoVO> queryWrapper = new QueryWrapper<UReportInfoVO>();
        queryWrapper.orderByDesc("time");
        Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page","sort","access_token");
        ICustomPage<UReportInfo> pageData = this.reportInfoService.page(queryParams, page);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(pageData));

    }

    /**获取举报信息
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "获取举报信息")
    @RequestMapping(value = {"/load/{id}"}, method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity loadReportInfo(@PathVariable("id") String id, HttpServletRequest request) {
        UReportInfo reportInfo = this.reportInfoService.loadReportInfoById(id);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(reportInfo)));

    }


    /**修改举报信息
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "修改举报信息")
    @RequestMapping(value = {"/update/{id}"}, method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity update(@PathVariable("id") String id, @RequestBody UReportInfoVO entity, HttpServletRequest request) {
        UReportInfo update = reportInfoService.updateReportInfo(id,entity);
        /*UReportInfo reportInfo = this.reportInfoService.load(id);
        CopyUtils.copyProperties(entity, reportInfo);
        UReportInfo update = this.reportInfoService.update(reportInfo);*/
        //UFileRelation load = this.reportFileService.queryByObjId(id);
        //UFileRelation uFileRelation = new UFileRelation();
        //uFileRelation.setId(load.getId());
        //uFileRelation.setType(entity.getType());
        //uFileRelation.setUrl(entity.getUrl());
        //CopyUtils.copyProperties(uFileRelation, load);
        //reportFileService.update(uFileRelation);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(update)));

    }


    /**删除举报信息
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "删除举报信息")
    @ResponseBody
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    public ResponseEntity deletes(@PathVariable("id") String id){
        reportInfoService.deleteReportInfo(id);
        /*this.reportInfoService.delete(id);
        UFileRelation load = this.reportFileService.queryByObjId(id);
        this.reportFileService.delete(load.getId().toString());*/
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(1));
    }
}
